﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Tester.Components;
using Xceed.Document.NET;
using Xceed.Words.NET;

namespace Tester
{
    public partial class FormBaogao : Form
    {
        private WTS wts = new WTS();
        private string fileFilter = "*.*|*.docx";
        public FormBaogao()
        {
            InitializeComponent();
        }

        private void toolStripButtonOpenWTSdocx_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK)
                return;

            //加载模板
            DocX document = DocX.Load(ofd.FileName);
            Table table1 = document.Tables[0];

            wts.cpmc = table1.Rows[0].Cells[1].Paragraphs[0].Text;

            wts.wtdwmc = table1.Rows[1].Cells[2].Paragraphs[0].Text;
            wts.wtdwdz = table1.Rows[2].Cells[2].Paragraphs[0].Text;
            string[] lxr = table1.Rows[3].Cells[2].Paragraphs[0].Text.Split(' ');
            wts.wtdwlxr = lxr[0];
            wts.wtdwlxrdh = lxr[lxr.Length-1];

            wts.scdwmc = table1.Rows[4].Cells[2].Paragraphs[0].Text;
            wts.scdwdz = table1.Rows[5].Cells[2].Paragraphs[0].Text;


            #region 回显信息
            textBoxcpmc.Text = wts.cpmc;
            
            textBoxwtdwmc.Text = wts.wtdwmc;
            textBoxwtdwdz.Text = wts.wtdwdz;
            textBoxwtdwlxr.Text = wts.wtdwlxr;
            textBoxwtdwlyrdh.Text = wts.wtdwlxrdh;

            textBoxscdwmc.Text = wts.scdwmc;
            textBoxscdwdz.Text = wts.scdwdz;
            #endregion

        }

        private void toolStripButtonOutput_Click(object sender, EventArgs e)
        {
            //加载模板
            DocX document = DocX.Load(@"C:\templateDocx.docx");

            //替换文档中标识
            document.ReplaceText("$value$", "根据模板生成文档");

            //根据表格模板填充表数据
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("Column1");
            dataTable.Columns.Add("Column2");
            dataTable.Rows[0]["Column1"] = "列1内容";
            dataTable.Rows[0]["Column2"] = "列2内容";
            DataTable resultNew = new DataTable();
            resultNew = dataTable.DefaultView.ToTable(false, new string[] { "Column1", "Column2" });
            int templateRowId = 1;   //从第2列填充数据
            Table table = document.Tables[0];    //模板文档中表格索引，从0开始
            MakeTableByTemplate(table, resultNew, templateRowId);



            //生成文档到指定目录
            document.SaveAs(@"C:\sourceFileName.docx");
        }

        private void MakeTableByTemplate(Table table, DataTable result, int templateRowId)
        {
            Row templateRow = table.Rows[templateRowId];
            int rowId = 0;
            foreach (DataRow row in result.Rows)
            {
                table.InsertRow(templateRow);
                int colId = 0;
                foreach (Cell cell in table.Rows[templateRowId + 1 + rowId].Cells)
                {
                    cell.ReplaceText("$d$", row[colId].ToString());

                    colId++;
                }
                rowId++;
            }
            table.RemoveRow(templateRowId);
        }

        private void 输出支持的所有标题级别()
        {
            using (DocX document = DocX.Create(@"C:\DocumentHeading.docx"))
            {
                foreach (HeadingType heading in (HeadingType[])Enum.GetValues(typeof(HeadingType)))
                {
                    string text = string.Format("{0} - The quick brown fox jumps over the lazy dog", heading.EnumDescription());

                    Paragraph p = document.InsertParagraph();
                    p.AppendLine(text).Heading(heading);
                }
                document.Save();
            }
        }

        private void 文档中插入表格(DocX document1)
        {
            Table table1 = document1.AddTable(1, 3); //一行三列
            table1.Design = TableDesign.TableGrid;    //表格样式
            table1.Alignment = Alignment.center;      //设置表格居中
            table1.Rows[0].Cells[0].Paragraphs[0].Append("列1").Bold();
            table1.Rows[0].Cells[1].Paragraphs[0].Append("列2").Bold();
            table1.Rows[0].Cells[2].Paragraphs[0].Append("列3").Bold();

            table1.Rows[0].Cells[0].Width = 100;   //设置单元格宽度
            table1.Rows[0].Cells[1].Width = 100;
            table1.Rows[0].Cells[2].Width = 100;


            Paragraph p = document1.InsertParagraph();
            p.Alignment = Alignment.center;
            p.Append("表格名称").Bold();
            p.InsertTableAfterSelf(table1);
        }

        private void toolStripButtonOpenWTSocr_Click(object sender, EventArgs e)
        {
            FormOCRwts formOCRwts = new FormOCRwts();
            if (formOCRwts.ShowDialog() == DialogResult.OK)
            {
                wts = formOCRwts.wts;


                #region 回显信息
                textBoxcpmc.Text = wts.cpmc;

                textBoxwtdwmc.Text = wts.wtdwmc;
                textBoxwtdwdz.Text = wts.wtdwdz;
                textBoxwtdwlxr.Text = wts.wtdwlxr;
                textBoxwtdwlyrdh.Text = wts.wtdwlxrdh;

                textBoxscdwmc.Text = wts.scdwmc;
                textBoxscdwdz.Text = wts.scdwdz;
                #endregion
            }

        }
    }
}
